package linkedList;

public class QuestionTwo {


    //合并两个升序链表 合并后也为升序
    public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
        ListNode head  = new ListNode(0);
        ListNode temp = head;
        while(list1 != null && list2 != null){
            if (list1.val <= list2.val){
                temp.next = list1;
                list1 = list1.next;
            }else{
                temp.next = list2;
                list2 = list2.next;
            }
            temp = temp.next;
        }
        temp.next = list1 == null?list2:list1;
        return head.next;
        /*ListNode temp1 = list1,temp2=list2;
        ListNode head = new ListNode(0);
        ListNode tempHead = head;
        while(temp1 != null || temp2 != null){
            ListNode temp;
            if(temp1.val < temp2.val){
                temp = temp1;
                temp1 = temp1.next;
            }else if(temp2.val <temp1.val){
                temp = temp2;
                temp2 = temp2.next;
            }else if(temp1 == null){
                temp = temp2;
                temp2 = temp2.next;
            }else{
                temp = temp1;
                temp1 = temp1.next;
            }
            tempHead.next = temp;
            tempHead = tempHead.next;
        }
        return head;*/
    }



}
